#!/bin/bash

USERNAME=$1
PASSWORD=$2
if [ "$USERNAME" == "" -o "$PASSWORD" == "" ]; then
    echo "Usage: $0 username password"
    exit 1
fi

echo "start create user $USERNAME"
SSHKEYFILE="/home/$USERNAME/.ssh/id_rsa"
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

sudo useradd -m -s /bin/bash $USERNAME
sudo -u $USERNAME mkdir -p /home/$USERNAME/.ssh
echo "${USERNAME}:${PASSWORD}" | sudo chpasswd

sudo -u $USERNAME ssh-keygen -b 2048 -t rsa -f $SSHKEYFILE -q -N ""
if [ $? -eq 0 ]; then
    echo "create user ${USERNAME} OK"
else
    echo "create user ${USERNAME} FAILED"
fi

